/** 
 * @(#)ProperyUtils.java 2013-5-21
 * 
 * Copyright (c) 1995-2013 Wonders Information Co.,Ltd. 
 * 1518 Lianhang Rd,Shanghai 201112.P.R.C.
 * All Rights Reserved.
 * 
 * This software is the confidential and proprietary information of Wonders Group.
 * (Social Security Department). You shall not disclose such
 * Confidential Information and shall use it only in accordance with 
 * the terms of the license agreement you entered into with Wonders Group. 
 *
 * Distributable under GNU LGPL license by gnu.org
 */

package com.xieguoking.tools;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * @author xieguojun
 * @version $Revision$ 2013-5-21
 * @author ($Date$ modification by $Author$)
 * @since 1.0
 */
public class ProperyUtils {

	final static Properties cache = new Properties();

	/**
	 * @param filename
	 */
	public static Properties loadProperties(String filename) {
		InputStream is = null;
		Properties props =new Properties();
		try {
			try {
				is = ProperyUtils.class.getClassLoader().getResourceAsStream(filename);
				props.load(is);
			}
			catch (FileNotFoundException e) {
				e.printStackTrace();
			}
			catch (IOException e) {
				e.printStackTrace();
			}
			finally {
				if (is != null)
					is.close();
			}
		}
		catch (IOException e) {
			e.printStackTrace();
		}

		return props;
	}

	public static Properties reload(String filename) {
		cache.clear();
		cache.putAll(loadProperties(filename));
		return cache;
	}

	public static Properties append(String filename) {
		cache.putAll(loadProperties(filename));
		return cache;
	}
	
	public static Properties getCache(){
		return cache;
	}

	private static void check() {
		/*if (cache.isEmpty())
			reload("relay_en.properties");*/
	}

	/**
	 * @param key
	 * @return
	 */
	public static String getValue(String key) {
		check();
		String value = cache.getProperty(key);
		return value == null ? key : value;
	}

}
